package com.pacy.swj.swjwb.Utils;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;

import h.framework.android.utils.JsonUtil;
import h.framework.android.utils.LogUtil;

/**
 * Created by pengx on 17/3/2.
 */

public class HttpUtils {
    /**
     * 发送get请求
     * @param <T>
     */
    public static <T> Callback.Cancelable Get(String header, String url, Map<String,String> map, Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(null!=map){
            for(Map.Entry<String, String> entry : map.entrySet()){
                params.addQueryStringParameter(entry.getKey(), entry.getValue());
            }
        }
        if(null!=header){
            params.addHeader("Authorization","Bearer "+header);
        }
        Callback.Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }

    /**
     * 发送put请求
     * @param <T>
     */
    public static <T> Callback.Cancelable Put(String header, String url, Map<String,String> map, Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(null!=map){
            for(Map.Entry<String, String> entry : map.entrySet()){
                params.addQueryStringParameter(entry.getKey(), entry.getValue());
            }
        }
        if(null!=header){
            params.addHeader("Authorization","Bearer "+header);
        }
        Callback.Cancelable cancelable = x.http().request(HttpMethod.PUT,params, callback);
        return cancelable;
    }

    /**
     * 发送delete请求
     * @param <T>
     */
    public static <T> Callback.Cancelable Delete(String header, String url, Map<String,String> map, Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(null!=map){
            for(Map.Entry<String, String> entry : map.entrySet()){
                params.addQueryStringParameter(entry.getKey(), entry.getValue());
            }
        }
        if(null!=header){
            params.addHeader("Authorization","Bearer "+header);
        }
        Callback.Cancelable cancelable = x.http().request(HttpMethod.DELETE,params, callback);
        return cancelable;
    }

    /**
     * 发送post请求
     * @param <T>
     */
    public static <T> Callback.Cancelable Post(String url, Map<String,Object> map, Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        params.addHeader("Content-Type","application/json");
        params.setBodyContent(JsonUtil.mapToJson(map).toString());
        LogUtil.i("参数---"+JsonUtil.mapToJson(map).toString());
        Callback.Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }


    /**
     * 发送post请求 带Header验证
     * @param <T>
     */
    public static <T> Callback.Cancelable PostForm( String url, Map<String,String> map, Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(null!=map){
            for(Map.Entry<String, String> entry : map.entrySet()){
                params.addBodyParameter(entry.getKey(), entry.getValue());
            }
        }
        LogUtil.i("参数---"+mapToJson(map));
        Callback.Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }


    /**
     * 将一个map转换成一个Json字符串
     *
     * @param map
     * @return
     */
    public static String mapToJson(Map<String, String> map) {

        return new Gson().toJson(map);
    }

    /**
     * 上传文件
     * @param <T>
     */
    public static <T> Callback.Cancelable UpLoadFile(String url, Map<String,Object> map, Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(null!=map){
            for(Map.Entry<String, Object> entry : map.entrySet()){
                params.addParameter(entry.getKey(), entry.getValue());
            }
        }
        params.setMultipart(true);
        Callback.Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }

    /**
     * 下载文件
     * @param <T>
     */
    public static <T> Callback.Cancelable DownLoadFile(String url, String filepath, Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        //设置断点续传
        params.setAutoResume(false);
        //自动命名
        params.setAutoRename(false);
        params.setSaveFilePath(filepath);
        Callback.Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }


    /**
     * 返回的信息
     */
    public static String getReason(String json){
        try {
            JSONObject object=new JSONObject(json);
            return object.getString("reason");
        } catch (JSONException e) {
            e.printStackTrace();
            return "未知错误";
        }
    }




}
